package com.destin.basic.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.activation.MimetypesFileTypeMap;
import java.io.File;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author ：destin
 * @date ：2022-03-23
 * @version: V1.0
 * @description: 文件处理工具类
 **/
public class FileUtil {

    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 文件后缀
     */
    public static final String FILE_SUFFIX_EXCEL = ".xlsx";
    public static final String FILE_SUFFIX_03EXCEL = ".xls";
    public static final String FILE_SUFFIX_WORD = ".doc";
    public static final String FILE_SUFFIX_WORD07 = ".docx";
    public static final String FILE_SUFFIX_PPT = ".ppt";
    public static final String FILE_SUFFIX_PPT07 = ".pptx";
    public static final String FILE_SUFFIX_WPS = ".wps";
    public static final String FILE_SUFFIX_WPT = ".wpt";
    public static final String FILE_SUFFIX_TXT = ".txt";
    public static final String FILE_SUFFIX_PDF = ".pdf";

    public static final String ZIP_SUFFIX = ".zip";
    public static final String RAR_SUFFIX = ".rar";

    public static final String FILE_SUFFIX_PNG = ".png";
    public static final String FILE_SUFFIX_JPG = ".jpg";
    public static final String FILE_SUFFIX_JPEG = ".jpeg";
    public static final String FILE_SUFFIX_GIF = ".gif";

    public static final String FILE_SUFFIX_OFD = ".ofd";
    public static final String FILE_SUFFIX_CEBX = ".cebx";

    public static final String MP4_SUFFIX = ".mp4";
    public static final String MP3_SUFFIX = ".mp3";

    public static final String FILE_SUFFIX_IMAGE_PNG = "image/png";
    public static final String ERROR_MESSAGE = "文件删除失败，文件：{}";
    public static final String SUCCESS_MESSAGE = "文件删除成功，文件：{}";

    public static List<String> FILE_MAP;

    static {
        FILE_MAP = Arrays.asList(FILE_SUFFIX_EXCEL, FILE_SUFFIX_03EXCEL, FILE_SUFFIX_WORD, FILE_SUFFIX_WORD07,
                FILE_SUFFIX_PPT, FILE_SUFFIX_PPT07, FILE_SUFFIX_WPS, FILE_SUFFIX_WPT, FILE_SUFFIX_TXT, FILE_SUFFIX_PDF,
                ZIP_SUFFIX, RAR_SUFFIX,
                FILE_SUFFIX_PNG, FILE_SUFFIX_JPG, FILE_SUFFIX_JPEG, FILE_SUFFIX_GIF, FILE_SUFFIX_OFD, FILE_SUFFIX_CEBX,
                MP4_SUFFIX, MP3_SUFFIX);
    }

    /**
     * 该方式支持本地文件，也支持http/https远程文件
     *
     * @param file
     */
    public static String getContentType(File file) {
        String contentType = new MimetypesFileTypeMap().getContentType(file);
        ;
        return contentType;
    }

    /**
     * 文件递归删除
     */
    public static void deleteFile(File directFile, String operation) {
        File[] files = directFile.listFiles();
        if (files != null && files.length > 0) {
            Stream.of(files).forEach(file -> {
                if (file.delete()) {
                    logger.info(operation + SUCCESS_MESSAGE, file.getAbsolutePath());
                } else {
                    logger.info(operation + ERROR_MESSAGE, file.getAbsolutePath());
                }
                if (file.isDirectory()) {
                    deleteFile(file, operation);
                }
            });
        }
        if (directFile.exists()) {
            if (directFile.delete()) {
                logger.info(operation + SUCCESS_MESSAGE, directFile.getAbsolutePath());
            } else {
                logger.info(operation + ERROR_MESSAGE, directFile.getAbsolutePath());
            }
        }
    }

    /**
     * 删除文件目录
     *
     * @param file 文件目录
     * @return boolean 执行结果
     */
    public static boolean deleteChildFiles(File file) {
        // 文件不存在或不是文件夹
        if (!file.exists() || !file.isDirectory()) {
            return false;
        }
        File[] files = file.listFiles();
        for (File f : files) {
            // 判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                deleteChildFiles(f);
            }
            if (f.delete()) {
                logger.info(SUCCESS_MESSAGE, f.getAbsolutePath());
            } else {
                logger.info(ERROR_MESSAGE, f.getAbsolutePath());
            }
        }
        return true;
    }

    /**
     * 文件大小转换
     *
     * @param size 文件大小
     * @return java.lang.String
     */
    public static String formatSize(long size) {
        if (size <= 0) {
            return "0 KB";
        }
        final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        DecimalFormat format = new DecimalFormat("#,##0.#");
        double sizeStr = size / Math.pow(1024, digitGroups);
        return format.format(sizeStr) + " " + units[digitGroups];
    }
}
